package personal.causeless.driver.reader;

import java.sql.Driver;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import bsh.This;

public class TestReader {
	
		WebDriver driver;
		
		public TestReader(WebDriver driver){
				this.driver = driver;
		}
		
		public String getCellValue(By by,String tableCellAddress,String attribute) {
			
			WebElement table = driver.findElement(by);
			
			Integer index = tableCellAddress.trim().indexOf(".");
			
			Integer row = Integer.parseInt(tableCellAddress.substring(0, index));
			
			Integer cell = Integer.parseInt(tableCellAddress.substring(index+1));
			
			List<WebElement> rows = table.findElements(By.tagName("ul"));
			
			WebElement theRow = rows.get(row);
			
			switch (attribute) {
			case "text":
				return getCell(theRow, cell).getText();
			case "href":
				return getCell(theRow, cell).getAttribute(attribute);
			case "title":
				return getCell(theRow, cell).getAttribute(attribute);
			case "name":
				return getCell(theRow, cell).getAttribute(attribute);
			case "class":
				return getCell(theRow, cell).getAttribute(attribute);
			case "id":
				return getCell(theRow, cell).getAttribute(attribute);
			default:
				return null;
			}
			
			
		}
		
		private WebElement getCell(WebElement row,Integer cell) {
			
			List<WebElement> cells;
			
			WebElement target = null;
			
			if (row.findElements(By.tagName("li")).size()>0) {
				
				cells = row.findElements(By.tagName("li"));
				
				target = cells.get(cell);
			}
			
			return target;
		}
		
		public static void main(String[] args) {
			
			String url = "http://snowboots.vancl.com/search?navtype=snowboots&cateid=28122&ref=ch_snowboots-hp-nav-snowboots";

			   WebDriver dr = new FirefoxDriver();
			        
			   		dr.get(url);
			        
			        TestReader table = new TestReader(dr);  
			        System.out.println(table.getCellValue(By.className("productList"), "0.2","href")); 
			        System.out.println(table.getCellValue(By.className("productList"), "2.1","href")); 
			        
			        dr.quit();
			
			
		}
		
		
	}
